package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.download.NotificationUpdateActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.DownloadVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.MyApplication;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

/**
 * 设置界面
 * @author jindandan
 */
public class SetActivity extends BaseActivity {
	@BindView(id = R.id.ll_set09, click = true)
	public LinearLayout ll_set09;//我的主页
	@BindView(id = R.id.ll_set08, click = true)
	public LinearLayout ll_set08;//积分活动
	@BindView(id = R.id.ll_set07, click = true)
	public LinearLayout ll_set07;// 关于应用
	@BindView(id = R.id.ll_set06, click = true)
	public LinearLayout ll_set06;// 使用帮助
	@BindView(id = R.id.ll_set05, click = true)
	public LinearLayout ll_set05;// 检查更新
	@BindView(id = R.id.ll_set04, click = true)
	public LinearLayout ll_set04;// 建议反馈
	@BindView(id = R.id.ll_set03, click = true)
	public LinearLayout ll_set03;// 应用评分
	@BindView(id = R.id.ll_set02, click = true)
	public LinearLayout ll_set02;// 分享好友
	@BindView(id = R.id.ll_set01, click = true)
	public LinearLayout ll_set01;// 更多精彩应用

	@BindView(id = R.id.txt_bbname, click = true)
	public TextView txt_bbname;// 更多精彩应用
	private MyApplication app;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_set);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		app = (MyApplication) getApplication();
		initTop();
		txt_bbname.setText("前版本 "+CommUtil.getAppInfo(aty));
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		Intent intent=null;
		switch (v.getId()) {
			case R.id.ll_set01:// 更多精彩应用
				intent=new Intent(aty,WonderfulAppActivity.class);
				break;
			case R.id.ll_set02:// 分享好友
				ToastUtil.showShort(aty, "分享好友");
				break;
			case R.id.ll_set03:// 应用评分
				ToastUtil.showShort(aty, "应用评分");
				break;
			case R.id.ll_set04:// 建议反馈
				if(TextUtils.isEmpty(LoginSp.getToken(aty))){
					StartIntent.startLogin(aty);
				}else{
					intent=new Intent(aty,AdviceActivity.class);
				}
				break;
			case R.id.ll_set05:// 检查更新
				http();
//			ToastUtil.showShort(aty, "检查更新");
				break;
			case R.id.ll_set06:// 使用帮助
				intent=new Intent(aty,HelpAndMessageActivity.class);
				intent.putExtra("type", 1);
				intent.putExtra("name", "使用帮助");
				break;
			case R.id.ll_set07:// 关于应用
				intent=new Intent(aty,HelpAndMessageActivity.class);
				intent.putExtra("type", 2);
				intent.putExtra("name", "关于应用");
				break;
			case R.id.ll_set08:// 积分活动
//			if(TextUtils.isEmpty(LoginSp.getToken(aty))){
//				StartIntent.startLogin(aty);
//			}else{
				intent=new Intent(aty,IntegralActivity.class);
//			}
				break;
			case R.id.ll_set09:// 我的主页
				if(TextUtils.isEmpty(LoginSp.getToken(aty))){
					StartIntent.startLogin(aty);
				}else{
					intent=new Intent(aty,SelfUserTwoActivity.class);
				}
				break;
		}
		if(intent!=null){
			startActivity(intent);
		}
	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("设置");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.INVISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	private void http() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(this, HttpInterface.FILE_VERSION, params,
				new AppHttpCallBack<DownloadVo>(DownloadVo.class,aty) {
					@Override
					public void onSuccess(DownloadVo result) {
						try {
							if (CommUtil.getVersionCode(aty) < result.info.fileVersion) {
								if (CommUtil.getVersionCode(aty) < result.info.fileMinVersion) {
									fileVersion(result.info.fileUrl,result.info.fileNote);
								} else {
									fileVersion(result.info.fileUrl,result.info.fileNote);
								}
							}
						} catch (Exception e) {
						}
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}

	private void fileVersion(final String url, String content) {
		AlertDialog.Builder builder = new AlertDialog.Builder(aty);
		builder.setTitle("更新");
		builder.setMessage(content);
		builder.setNegativeButton("确定", new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				app.setDownload(true);
				app.setApkUrl(url);
				Intent it = new Intent(aty,NotificationUpdateActivity.class);
				startActivity(it);

			}
		});
		builder.setNeutralButton("继续使用", null);
		builder.show();
	}
}
